package br.com.fogas.database.dao.impl;

import java.sql.SQLException;
import java.util.List;

import br.com.fogas.database.IMetadata.IMaterial;
import br.com.fogas.database.dao.IMaterialDao;
import br.com.fogas.database.entidade.Material;
import br.com.fogas.webservice.parser.material.MaterialResponseItem;

import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.support.ConnectionSource;

public class MaterialDao extends BaseDaoImpl<Material, Integer> implements IMaterialDao {

	public MaterialDao(ConnectionSource connectionSource) throws SQLException {
		super(connectionSource, Material.class);
	}

	@Override
	public void sincronizar(List<MaterialResponseItem> items) throws SQLException {
		for (MaterialResponseItem materialResponseItem : items) {
			create(new Material(materialResponseItem));
		}
	}

	@Override
	public Material listarMaterialPorCodigo(String codigo) throws SQLException {
		List<Material> query = queryBuilder().where()
		.eq(IMaterial.tx_cod_material, codigo)
		.query();

		if(query.size() == 1) {
			return query.get(0);
		} else {
			return null;
		}
	}
}
